package com.sakura.mall.order.paymentInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.paymentInfo.domain.entity.PaymentInfoEntity;
import com.sakura.mall.order.paymentInfo.domain.request.AddPaymentInfoRequest;
import com.sakura.mall.order.paymentInfo.domain.request.UpdatePaymentInfoRequest;
import com.sakura.mall.order.paymentInfo.domain.request.PaymentInfoPageRequest;
import com.sakura.mall.order.paymentInfo.domain.vo.PaymentInfoVo;
import com.sakura.mall.order.paymentInfo.mapper.PaymentInfoMapper;
import com.sakura.mall.order.paymentInfo.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支付信息表 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfoEntity> implements PaymentInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddPaymentInfoRequest request) {
        // todo 其他校验逻辑
        PaymentInfoEntity paymentInfo = request.convert(PaymentInfoEntity.class);
        this.save(paymentInfo);
    }

    @Override
    public Page<PaymentInfoVo> queryPage(PaymentInfoPageRequest request) {
        IPage<PaymentInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<PaymentInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<PaymentInfoVo> ret = qp.convert(entity -> entity.convert(PaymentInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdatePaymentInfoRequest request) {
        PaymentInfoEntity entity = request.convert(PaymentInfoEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

